#include "user_manager.h"
#include <unordered_map>
#include <vector>

static std::unordered_map<int, std::string> fd_to_name;
static std::unordered_map<std::string, int> name_to_fd;

void add_user(int fd, const std::string& nickname) {
    fd_to_name[fd] = nickname;
    name_to_fd[nickname] = fd;
}

void remove_user(int fd) {
    if (fd_to_name.count(fd)) {
        std::string name = fd_to_name[fd];
        name_to_fd.erase(name);
        fd_to_name.erase(fd);
    }
}

bool is_nickname_taken(const std::string& nickname) {
    return name_to_fd.count(nickname) > 0;
}

std::string get_nickname(int fd) {
    return fd_to_name.count(fd) ? fd_to_name[fd] : "";
}

int get_fd_by_name(const std::string& nickname) {
    return name_to_fd.count(nickname) ? name_to_fd[nickname] : -1;
}

std::vector<std::string> get_all_nicknames() {
    std::vector<std::string> names;
    for (const auto& pair : name_to_fd) {
        names.push_back(pair.first);
    }
    return names;
}

